/**
 * 
 */
package nl.samenloop.bigscreen.sequence;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Calendar;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nl.samenloop.bigscreen.Constants;
import nl.samenloop.bigscreen.communication.ClockReader;
import nl.samenloop.bigscreen.communication.Result;

/**
 * @author Bram Wiekens
 * 
 */
public class ClockSequence extends Sequence {

	private static long DAY = 24 * 3600000;
	private static long HOUR = 3600000;
	private static long MINUTE = 60000;
	private static long SECOND = 1000;
	private static long start;
	private JLabel clock;

	SequenceStartListener listener;

	public ClockSequence(DrawingTarget target, SequenceStartListener listener) {
		super(target, "");

		this.listener = listener;
	}

	@Override
	public void start() {
		Container c = target.getCenterContainer();

		JComponent time = createLabel("24:00:00", 130);
		time.setBounds(80, 50, time.getPreferredSize().width, time.getPreferredSize().height);

		c.add(time);

		start(getTitle(Constants.CLOCK_TITLE));

		waitForStart();

	}

	
	public long getStart(){
		return start;
	}
	
	protected JComponent createLabel(String text, int size) {

		FlowLayout fl = new FlowLayout(FlowLayout.CENTER);

		JPanel c = new JPanel(true);
		c.setBackground(Constants.PURPLE);
		// c.setBackground(BigScreen.TRANSPARANT);
		c.setLayout(fl);

		// String msg = String.format("<html><div WIDTH=%d>%s</div><html>",
		// c.getWidth() , text);
		clock = new JLabel(text);

		clock.setFont(new Font("Droid Sans", Font.BOLD, size));
		// label.setLineWrap(wrap);
		clock.setForeground(Color.white);

		c.add(clock);

		return c;
	}

	public void waitForStart() {

		final ClockReader cr = new ClockReader(Constants.getProperty(Constants.CLOCK_URL));
		final Thread t = new Thread(cr);

		cr.setResultHandler(new Result<Long>() {

			@Override
			public void onSucces(Long result) {
				start = result + DAY;
				startUpdateThread();
			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

		t.start();

	}

	@Override
	public void prepare() {
		// TODO Auto-generated method stub

	}

	public void startUpdateThread() {
		Thread updater = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					clock.setText(updateClock());
					try {
						Thread.sleep(1000);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					clock.invalidate();
				}
			}
		});

		updater.start();
		
		try {
			Thread.sleep(120000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listener.start();
	}

	private String prepend(long num) {
		return num < 10 ? "0" + num : "" + num;
	}

	private String setClock(long days, long hours, long minutes, long seconds) {
		String dayStr = days > 0 ? days + " dagen " : "";
		String time = "";

		time = dayStr + prepend(hours) + ":" + prepend(minutes) + ":" + prepend(seconds);

		return time;
	}

	private String updateClock() {
		long now = Calendar.getInstance().getTimeInMillis();

		long diff = start - now > 0 ? start - now : 0;

		long days = diff / DAY;

		diff = diff - (days * DAY);

		long hours = diff / HOUR;

		diff = diff - (hours * HOUR);

		long minutes = diff / MINUTE;

		diff = diff - (minutes * MINUTE);

		long seconds = diff / SECOND;

		return setClock(days, hours, minutes, seconds);
	}

}
